import require from '@/utils/request'
import { SAVE_CHANNEL, SAVE_ALL_CHANNELS, SAVE_ARTICLE_LIST } from '@/store/action_types/home'
import { getLocalChannels, hasToken, setLocalChannels } from '@/utils/storage'

/**
 * 
 * @returns 获取用户的频道
 */
export const getUserChannels = () => {
    return async dispath => {
        // 1. 判断用户是否登录
        if(hasToken()) {
            const res = await require.get('/user/channels') 
            dispath(saveUserChannels(res.data.channels))
            setLocalChannels(res.data.channels)
        }else {
            // 2. 没有token, 从本地获取频道数据
            const channels = getLocalChannels()
            if(channels) {
                dispath(saveUserChannels(channels))
            }else {
                // 3. 没有登录, 且本地没有channels数据
                const res = await require.get('/user/channels') 
                dispath(saveUserChannels(res.data.channels))
                // 保存到本地
                setLocalChannels(res.data.channels)
            }
        }
        
    }
}

export const saveUserChannels = (payload) => {
    return {
        type: SAVE_CHANNEL,
        payload: payload
    }
}

export const getAllChannels = () => {
    return async dispatch => {
        const res = await require.get('/channels')

        dispatch({
            type: SAVE_ALL_CHANNELS,
            payload: res.data.channels
        })
    }
}

export const delChannel = (channel) => {
    return async (dispatch, getState) => {
        // 如果用户登录, 需要发送请求删除频道
        // 如果用户没有登录, 需要删除本地中的这个频道
        // 不管登录没登录, 都需要修改redux中的频道
        const userChannels = getState().home.userChannels
        if(hasToken()) {
            // 发送请求
            await require.delete(`/user/channels/${channel.id}`)   
            // 同步频道数据到redux中
            dispatch(saveUserChannels(userChannels.filter(item => item.id !== channel.id)))
        }else {
            // 没有登录
            // 修改本地, 修改redux
            const result = saveUserChannels(userChannels.filter(item => item.id !== channel.id))
            dispatch(saveUserChannels(result.payload))
            setLocalChannels(result.payload)
        }   
    }
}

export const addChannel = (channel) => {
    return async (dispath, getState) => {
        const userChannels = getState().home.userChannels
        userChannels.push(channel)
        if(hasToken()) {
            await require.patch('/user/channels', {
                channels: [channel]
            })

            dispath(saveUserChannels(userChannels))
        }else {
            dispath(saveUserChannels(userChannels))
            setLocalChannels(userChannels)
        }
    }
}

export const getArticleList = (channelId, timestamp, loadMore= false) => {
    return async dispatch => {
        const res = await require({
            method: 'get',
            url: '/articles',
            params: {
                timestamp: timestamp,
                channel_id: channelId
            }
        })

        dispatch(setArticleList({
            channelId,
            timestamp: res.data.pre_timestamp,
            list: res.data.results,
            loadMore
        }))
    }
}

export const setArticleList = (payload) => {
    return {
        type: SAVE_ARTICLE_LIST,
        payload
    }
}

export const setMoreAction = (payload) => {
    return {
        type: 'home/setMoreAction',
        payload
    }
}

export const unLikeArticle = (id, channelId) => {
    return async (dispatch, getState) => {
        const res = await require({
            method: 'post',
            url: '/article/dislikes',
            data: {
                target: id
            }
        })

        const articles = getState().home.articles[channelId]

        dispatch(setArticleList({
            channelId,
            timestamp: articles.timestamp,
            list: articles.list.filter(item => item.art_id !== id)
        }))
    }
}

export const reportArtilce = (articleId, id, channelId) => {
    return async (dispatch, getState) => {
        await require({
            method: 'post',
            url: '/article/reports',
            data: {
                target: articleId,
                type: id
            }
        })

        const articles = getState().home.articles[channelId]

        dispatch(setArticleList({
            channelId,
            timestamp: articles.timestamp,
            list: articles.list.filter(item => item.art_id !== articleId)
        }))
    }
}